#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char** argv){
    int flag = 0;
    char opt;
    char buf[1024];
    memset(buf, 0, 1024);
    while((opt = getopt(argc, argv, "m:")) != -1){
        switch(opt){
            case 'm':
                printf("have m\n");
                flag = 1;
                strncpy(buf, optarg, 1024);
                break;
            default:
                fprintf(stderr, "Usage : %s -m msg\n", argv[0]);
                break;
        }
    }
    if(flag == 1){
        printf("%s\n", buf);
        return 0;
    }
    pid_t pid;
    if((pid = fork()) < 0){
        perror("fork()");
        return 1;
    }
    if(pid == 0){
        printf("i will use vim\n");
        sleep(2);
        execl("/usr/bin/vim", "/usr/bin/vim", "tmp.txt", NULL);
        return 0;
    }
    else{
        printf("i am father\n");
        wait(NULL);
        execl("/bin/cat", "cat", "tmp.txt", NULL);
    }
    return 0;
}